﻿using IDAL;

namespace DAL
{
    public class YkDal<T> : IYkDal<T> where T : class
    {
        public YkDal(MyDbContext context)
        {
            Context = context;
        }

        public MyDbContext Context { get; }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="t">表名</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> Add(T t)
        {
            await Context.Set<T>().AddAsync(t);
            return await Context.SaveChangesAsync();
        }
        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="t">表名</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> PlAdd(List<T> t)
        {
            await Context.Set<T>().AddRangeAsync(t);
            return await Context.SaveChangesAsync();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="t">表名</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> Delete(T t)
        {
            Context.Set<T>().Remove(t);
            return await Context.SaveChangesAsync();
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="t">表名</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> PlDelete(List<T> t)
        {
            Context.Set<T>().RemoveRange(t);
            return await Context.SaveChangesAsync();
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="t">表名</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> Update(T t)
        {
            Context.Set<T>().Update(t);
            return await Context.SaveChangesAsync();
        }
        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="t">表名</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> PlUpdate(List<T> t)
        {
            Context.Set<T>().UpdateRange(t);
            return await Context.SaveChangesAsync();
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>
        public IQueryable<T> Show()
        {
            return Context.Set<T>();
        }
    }
}
